#include "qipan.h"
#include <QDebug>
#include <QPoint>
qipan::qipan(QWidget *parent):
    m_hoverFlag(false),
    QWidget (parent)
{
    //清除脏数据
    m_board = (Role **) malloc(sizeof (Role *)* m_boardSize);

    for (int idx = 0; idx < m_boardSize; idx++)
    {
        m_board[idx] = (Role *) malloc(sizeof (Role) * m_boardSize);
    }

    for (int row = 0; row < m_boardSize; row++)
    {
        for (int col = 0; col < m_boardSize; col++)
        {
            m_board[row][col] = EMPTY;
        }
    }

    //捕获鼠标移动
    setMouseTracking(true);
}

void qipan::mouseMoveEvent(QMouseEvent *event)
{

    int row = (event->x() - m_margin + m_cellSize / 2) / m_cellSize;
    int col = (event->y() - m_margin + m_cellSize / 2) / m_cellSize;

    //判断是否在棋盘内部
    if((row >= 0 && row < m_boardSize) && (col >= 0 && col < m_boardSize))
    {
        if(m_hoverRow != row || m_hoverCol != col)
        {
            m_hoverRow = row;
            m_hoverCol = col;

            m_hoverFlag = true;
            //更新画面
            update();
        }
    }
    else
    {
        if(m_hoverFlag)
        {
            m_hoverFlag = false;

            //移出棋盘需要将坐标清空
            m_hoverRow = m_hoverCol = -1;

            //更新画面
            update();
        }
    }
}

void qipan::mousePressEvent(QMouseEvent *event)
{
    int row = (event->x() - m_margin + m_cellSize / 2) / m_cellSize;
    int col = (event->y() - m_margin + m_cellSize / 2) / m_cellSize;

    if((row >= 0 && row < m_boardSize) && (col >= 0 && col < m_boardSize))
    {
        if(m_board[row][col] == EMPTY)
        {
            emit ChessClick(row, col);
#if 0
            m_board[row][col] = PLAYER;
            update();
#endif
        }
    }
}

void qipan::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    //启用抗锯齿效果
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制棋盘背景
    // 棋盘背景颜色
    QColor backgroundColor = QColor(255, 240, 200);
    painter.fillRect(rect(), backgroundColor);

    //绘制棋盘的网格线
    for (int idx = 0; idx < m_boardSize; idx++)
    {
        /*绘制竖线*/
        painter.drawLine(m_margin + idx * m_cellSize, m_margin, m_margin + idx * m_cellSize, m_margin + (m_boardSize - 1) * m_cellSize);
        /*绘制横线*/
        painter.drawLine(m_margin, m_margin + idx * m_cellSize, m_margin + (m_boardSize - 1) * m_cellSize, m_margin + idx * m_cellSize);

    }

    //绘制悬停点
    if (m_hoverFlag &&
        (m_hoverRow >= 0 && m_hoverRow < m_boardSize) &&
        (m_hoverCol >= 0 && m_hoverCol < m_boardSize) &&
        (m_board[m_hoverRow][m_hoverCol] == EMPTY))
    {
        painter.setBrush(Qt::red);
        painter.drawEllipse(QPoint(m_margin + m_hoverRow * m_cellSize, m_margin + m_hoverCol * m_cellSize), m_cellSize / 6, m_cellSize / 6);
    }

    // 绘制黑棋、白棋
    for (int row = 0; row < m_boardSize; row++)
    {
        for (int col = 0; col < m_boardSize; col++)
        {
            if(m_board[row][col] != EMPTY)
            {
                if(m_board[row][col] == PLAYER)
                {
                    painter.setBrush(Qt::black);
                }
                else if(m_board[row][col] == COMPUTER)
                {
                    painter.setBrush(Qt::white);
                }

                painter.drawEllipse(QPoint(m_margin + row * m_cellSize, m_margin + col * m_cellSize), m_cellSize / 3, m_cellSize / 3);

            }
        }
    }
}

void qipan::setBoardInfo(int row, int col, Role role)
{
    if((row >= 0 && row < m_boardSize) && (col >= 0 && col < m_boardSize))
    {
        m_board[row][col] = role;
    }
}

Role **qipan::getBoardInfo(int &boardSize)
{
    boardSize = m_boardSize;
    return reinterpret_cast<Role**>(m_board);
}

int qipan::getBoardSize()
{
    return m_boardSize;
}

void qipan::clearBoard()
{
    for (int row = 0; row < m_boardSize; row++)
    {
        for (int col = 0; col < m_boardSize; col++)
        {
            m_board[row][col] = EMPTY;
        }
    }
}




